<template>
  <el-container>
    <basic-crud :option="option" businessUrl="/obj/busServiceController">
      <template #menu="{ row }">
        <el-button
          link
          type="primary"
          @click="handleScanObject(row)"
          :loading="row.loading"
          >扫描对象文件</el-button
        >
      </template>
    </basic-crud>
    <el-dialog title="扫描对象进度" v-model="visible">
      <el-table :data="tableData" border max-height="400px" v-loading="tableLoading">
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="状态" prop="status">
          <template #default="{ row }">
            <span>{{ statusMap[row.status] }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </el-container>
</template>

<script setup>
import { apiScanObject, apiGetScanProgress } from '@/apis/system/busService.js'
// import { getUuid } from '@/utils'
import { ref } from 'vue'
const option = {
  searchShow:false,
  calcHeight:10,
  updateBtn: true,
  selection: false,
  dialogWidth: '60%',
  simplePage: true,
  menuWidth: 280,
  labelWidth: 150,
  column: [
    {
      label: '服务编号',
      prop: 'code',
      search: true,
      rules: [{ required: true, message: '请输入服务编号', trigger: 'blur' }],
    },
    {
      label: '服务名称',
      prop: 'name',
      search: true,
      rules: [{ required: true, message: '请输入服务名称', trigger: 'blur' }],
    },
    {
      label: '前端请求路径',
      prop: 'requestPath',
      rules: [
        { required: true, message: '请输入前端请求路径', trigger: 'change' },
      ],
    },
    {
      label: '包名',
      prop: 'packageName',
      rules: [{ required: true, message: '请输入包名', trigger: 'change' }],
    },
  ],
}

const statusMap = {
  Executing: '执行中',
  finish: '已完工',
  fail: '失败终止',
  hangup: '等待执行',
}

const tableData = ref([])
const tableLoading = ref(false)
const curRow = ref()
const visible = ref(false)
const handleScanObject = async (row) => {
  row.loading = true
  curRow.value = row
  // curRow.value.uuid = getUuid()
  await apiScanObject(row.requestPath, {
    id: curRow.value.id,
    uuid: curRow.value.uuid,
  })
  row.loading = false
  visible.value = true
  tableLoading.value = true
  await apiGetScanProgress(row.requestPath, { uuid: curRow.value.uuid })
  timer.value = setInterval(async () => {
    await getScanProgress()
    tableLoading.value = false
  }, 1000)
}

const timer = ref()
const getScanProgress = async () => {
  const data = await apiGetScanProgress(curRow.value.requestPath, {
    uuid: curRow.value.uuid,
  })
  tableData.value = data
}

watch(
  () => visible.value,
  (val) => {
    if (!val) {
      clearInterval(timer.value)
      timer.value = null
    }
  }
)
</script>

<style lang="scss" scoped></style>
